package fhq.hcmute.edu.vn.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import fhq.hcmute.edu.vn.languages.Language;
import fhq.hcmute.edu.vn.util.ProjectUtil;

public class LanguageActivity extends Activity {
	private Button btnOk;
	private Button btnCancel;
	private RadioButton rbEnglish;
	private RadioButton rbVietnamese;

	@Override
	protected final void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Language.updateLanguageSystem(getBaseContext());
		setContentView(R.layout.activity_change_languages);
		init();
		updateRadioButton();
	}

	private void init() {
		btnOk = (Button) findViewById(R.id.btnOK);
		btnOk.setOnClickListener(onClickOkButton);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		btnCancel.setOnClickListener(onClickOkCancel);
		rbEnglish = (RadioButton) findViewById(R.id.rbEnglish);
		rbVietnamese = (RadioButton) findViewById(R.id.rbVietnamese);
	}

	private void updateRadioButton() {
		SharedPreferences userLanguage = getSharedPreferences(
				Language.USER_LANGUAGE, 0);
		String lang = userLanguage.getString(
				Language.KEY_LANGUAGE, Language.DEFAULT_LANGUAGE);
		if (lang.equals(Language.English)) {
			rbEnglish.setChecked(true);
		} else {
			rbVietnamese.setChecked(true);
		}
	}

	private OnClickListener onClickOkButton = new View.OnClickListener() {

		@Override
		public void onClick(final View v) {
			String language = null;
			if (rbEnglish.isChecked()) {
				language = Language.English;

			} else {
				language = Language.Vietnamese;
			}
			Intent data = new Intent();
			Bundle myBundle = new Bundle();
			myBundle.putString(Language.KEY_LANGUAGE, language);
			data.putExtras(myBundle);
			setResult(ProjectUtil.RESULT_CODE_PICK_LANGUAGE_CHANGE, data);
			finish();
		}
	};
	private OnClickListener onClickOkCancel = new View.OnClickListener() {

		@Override
		public void onClick(final View v) {
			setResult(ProjectUtil.RESULT_CODE_PICK_LANGUAGE_NONE);
			finish();
		}
	};
}
